/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cmdhelpers;

import commands.ICommandContextuelle;
import general.ConstantesConsole;

import java.text.MessageFormat;
import java.util.ResourceBundle;
import java.util.Locale;

/**
 *
 * eCOM - Adopte un Presta - Mars-Avril 2013
 * @author Kheira, Jef, William, Tuan-Long
 */
public class TranslateHelper {
    private final String resourceBundleBaseName;
    private final ICommandContextuelle command;

    public TranslateHelper(ICommandContextuelle command, String resourceBundleBaseName) {
        this.command = command;
	this.resourceBundleBaseName = resourceBundleBaseName;
    }

    public TranslateHelper(ICommandContextuelle command) {
	this(command, command.getClass().getPackage().getName() 
                + ".i18n." + command.getClass().getSimpleName());
    }
	
    public String translate(String key, Object ... messageArgs) {
		ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleBaseName, 
                        getLocale(), this.getClass().getClassLoader());

		return MessageFormat.format(bundle.getString(key), messageArgs);
    }

    public Locale getLocale() 
    {
        Locale contextLocale = (Locale) command.getContext().getVar(ConstantesConsole.LANGUAGE.name());

	return (contextLocale != null) ? contextLocale : Locale.getDefault();
    }

    public void setLocale(Locale locale) {
        command.getContext().setVar(ConstantesConsole.LANGUAGE.name(), locale);
    }

}
